package smartcampus.scjava.controller.third;

import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import smartcampus.scjava.POJO.DO.Xinzitongji;
import smartcampus.scjava.config.Result;
import smartcampus.scjava.service.XinzitongjiService;
import smartcampus.scjava.service.XueshengService;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/xinzitongji")
public class XinzitongjiController {
    @Resource
    private XinzitongjiService xinzitongjiService;
    @Resource
    private XueshengService xueshengService;

    /**
     * 分页条件查询
     * @param pageNum
     * @param pageSize
     * @param xinzitongji
     * @return
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "2") Integer pageSize,
                             Xinzitongji xinzitongji){
        PageInfo<Xinzitongji> pageInfo = xinzitongjiService.selectPage(pageNum, pageSize,xinzitongji);
        return Result.success(pageInfo);
    }

    /**
     * 新增数据
     * @param xinzitongji
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Xinzitongji xinzitongji){
        System.out.println(xinzitongji);
        //取出学号
        String xuehao = xinzitongji.getXuehao();
        //根据学号查询姓名
        String name = xueshengService.selectNameByXuehao(xuehao);
        //将姓名添加到xinzitongji
        xinzitongji.setXingming(name);
        xinzitongjiService.add(xinzitongji);
        return Result.success();
    }

    /**
     * 删除薪资信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        xinzitongjiService.deleteById(id);
        return Result.success();
    }

    /**
     * 更新薪资信息
     * @param xinzitongji
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Xinzitongji xinzitongji){
        xinzitongjiService.updateById(xinzitongji);
        return Result.success();
    }

    /**
     * 获取还未填入薪资且已就业的学生的学号
     * @return
     */
    @GetMapping("/selectXuehao")
    public Result selectXuehao(){
        // 获取所有薪资信息表的学号
        List<String> xinzi_xuehaos = xinzitongjiService.selectAllXuehao();
        // 获取所有已就业学生信息表的学号
        List<String> xuehaos = xueshengService.selectAllXuehao();

        // 将两个列表转换为HashSet
        Set<String> xinzi_xuehaos_set = new HashSet<>(xinzi_xuehaos);
        Set<String> xuehaos_set = new HashSet<>(xuehaos);

        // 取出两个集合不同的数据
        List<String> list = new ArrayList<>();
        for (String str : xinzi_xuehaos_set) {
            if (!xuehaos_set.contains(str)) {
                list.add(str);
            }
        }
        for (String str : xuehaos_set) {
            if (!xinzi_xuehaos_set.contains(str)) {
                list.add(str);
            }
        }

        String[] result = list.toArray(new String[0]);
        List<String> stringList = Arrays.stream(result).collect(Collectors.toList());
        return Result.success(stringList);
    }
}
